package com.cy.train.business.service.imp;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.cy.train.business.domain.TrainStation;
import com.cy.train.business.domain.TrainStationExample;
import com.cy.train.business.mapper.TrainStationMapper;
import com.cy.train.business.req.TrainStationQueryReq;
import com.cy.train.business.req.TrainStationSaveReq;
import com.cy.train.business.resp.TrainStationQueryResp;
import com.cy.train.business.service.TrainStationService;
import com.cy.train.common.exception.BusinessException;
import com.cy.train.common.exception.BusinessExceptionEnum;
import com.cy.train.common.resp.PageResp;
import com.cy.train.common.util.SnowUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description:
 * @Date: 2024/2/27 14:48
 * @Author: cyzhang
 */
@Service
public class TrainStationServiceImp implements TrainStationService {
    private static final Logger LOG = LoggerFactory.getLogger(TrainStationServiceImp.class);

    @Autowired
    private TrainStationMapper trainStationMapper;

    @Override
    public void save(TrainStationSaveReq req) {
        DateTime now = DateTime.now();
        TrainStation trainStation = BeanUtil.copyProperties(req, TrainStation.class);
        if(ObjectUtil.isNull(trainStation.getId())) {
            TrainStation uniqueTrainStation = selectUnique(req.getTrainCode(), req.getIndex());
            TrainStation uniqueTrainStation2 = selectUnique2(req.getTrainCode(), req.getName());
            if(BeanUtil.isNotEmpty(uniqueTrainStation)) {
                throw new BusinessException(BusinessExceptionEnum.TRAIN_STATION_CODE_INDEX_UNIQUE_ERROR);
            }
            if(BeanUtil.isNotEmpty(uniqueTrainStation2)) {
                throw new BusinessException(BusinessExceptionEnum.TRAIN_STATION_CODE_NAME_UNIQUE_ERROR);
            }
            trainStation.setId(SnowUtil.getSnowflakeNextId());
            trainStation.setCreateTime(now);
            trainStation.setUpdateTime(now);
            trainStationMapper.insert(trainStation);
        } else {
            trainStation.setUpdateTime(now);
            trainStationMapper.updateByPrimaryKeySelective(trainStation);
        }
    }

    private TrainStation selectUnique(String code, Integer index) {
        TrainStationExample trainStationExample = new TrainStationExample();
        trainStationExample.createCriteria().andTrainCodeEqualTo(code).andIndexEqualTo(index);
        List<TrainStation> list = trainStationMapper.selectByExample(trainStationExample);
        if(CollUtil.isNotEmpty(list)) {
            return list.get(0);
        } else {
            return null;
        }
    }
    private TrainStation selectUnique2(String code, String name) {
        TrainStationExample trainStationExample = new TrainStationExample();
        trainStationExample.createCriteria().andTrainCodeEqualTo(code).andNameEqualTo(name);
        List<TrainStation> list = trainStationMapper.selectByExample(trainStationExample);
        if(CollUtil.isNotEmpty(list)) {
            return list.get(0);
        } else {
            return null;
        }
    }

    @Override
    public PageResp<TrainStationQueryResp> queryList(TrainStationQueryReq req) {
        TrainStationExample trainStationExample = new TrainStationExample();
        trainStationExample.setOrderByClause("id desc");

        LOG.info("当前页：{}", req.getPage());
        LOG.info("每页数量：{}", req.getSize());

        PageHelper.startPage(req.getPage(), req.getSize());
        List<TrainStation> trainStationList = trainStationMapper.selectByExample(trainStationExample);

        PageInfo<TrainStation> pageInfo = new PageInfo<>(trainStationList);
        LOG.info("总条数：{}", pageInfo.getTotal());
        LOG.info("总页数：{}", pageInfo.getPages());

        List<TrainStationQueryResp> trainStationQueryRespList = BeanUtil.copyToList(trainStationList, TrainStationQueryResp.class);
        PageResp<TrainStationQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(trainStationQueryRespList);

        return pageResp;
    }

    @Override
    public void delete(Long id) {
        trainStationMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<TrainStation> queryTrainStationByCode(String trainCode) {
        TrainStationExample trainStationExample = new TrainStationExample();
        trainStationExample.setOrderByClause("`index` asc");
        trainStationExample.createCriteria().andTrainCodeEqualTo(trainCode);
        return trainStationMapper.selectByExample(trainStationExample);
    }
}
